package jsontest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MainTest {

    private static String URL_MODULE="/lekan/apimodulecontrol_json.so";
    private static String URL_LETVURL="/lekan/apiletvurl_json.so?mmsid=1913412&vtype=1";
    private static String URL_MID="/lekan/apimid_json.so?vid=1136075";
    private static String URL_RELATEDVIDEO="/lekan/apirelatedvideo_json.so?cid=1&pid=40494";
    private static String URL_GUESSLIKE="/lekan/apiguesslike_json.so?history=&random=";
    private static String URL_LANDING="/lekan/apilanding_json.so";
    private static String URL_SEARCH="/lekan/apisearch_json.so?wd=%E5%A4%A9%E9%BE%99%E5%85%AB%E9%83%A8&dt=1,2,3,4&from=mobile&pn=1&ps=2&stat=album&ph=420003,420004,-131&cg=1";
    private static String URL_SO="/lekan/apiso_json.so?user_id=66daa634-7d83-3772-b978-6a3054a20bd8&dt=6&ec=&ph=420003%2C420004%2C-131&pn=1&ps=20&wd=赵薇&from=mobile&stat=album&platform=";
    private static String URL_SPORTSTAR="/lekan/apisportstar_json.so?dt=2&src=1&subCategory=30238&or=1&sf=playerId&from=mobile&wd=691&pn=1&ps=8&client_ip=118.114.180.90";
    private static String URL_STARDETAIL="/lekan/apistardetail_json.so?album_filter=category%3A1%7C2%7C5%7C11&video_filter=category%3A9%7C11&dt=album%2Cvideo&jf=3&ph=420003%2C420004%2C-131&pn=1&ps=9&sa=%E8%B6%99%E8%96%87&sf=starringName%2CdirectoryName%2CactorName&from=mobile_09&lang=zh_TW&leId=251";
    private static String URL_STARWORK="/lekan/apistarwork_json.so?dt=album,video&album_filter=category:1|2|5|11&video_filter=category:9&jf=3&or=3&pn=1&ps=9&ph=420003,420004,-131&sf=starringName,directoryName,actorName&sa=%E5%88%98%E5%BE%B7%E5%8D%8E&from=mobile";
//    private static String URL_SUGGEST="/lekan/apisuggest_json.so?wd=12&lang=zh_cn";
    private static String URL_SUPERSO="/lekan/apisuperso_json.so?mix=2&wd=%E6%88%91%E7%9A%84%E7%81%B5%E7%95%8C%E7%94%B7%E5%8F%8B&pn=1&ps=20&ph=420003&from=mobile_super18&dt=1,2,3,4,5,7,8,9&lang=zh_cn&limit_rst=1";
    private static String URL_SULANDING="/lekan/apisulanding_json.so?terminalApplication=supersearch&appVersion=1.0.4&terminalBrand=letv&mac=84730345D3B6&bsChannel=letv_supersearch&terminalSeries=Letv_X500_default&devKey=5831b6d7d339ebae3b5e5f0996c15c91&pcode=160110000&wcode=cn&devId=868256020019948";
    private static String URL_SOKITS="/lekan/apisokits_json.so";
//    private static String URL_WEBSITE="/lekan/apiwebsite_json.so?sk=leso_a_282382_tudou";
    private static String URL_DETAIL="/lekan/apidetail_json.so?pn=2&ps=50&sk=mms_leso_a_289975";
    
    public static void main(String[] args) {
        MainTest t=new MainTest();
        t.domain("http://10.150.140.85:8088/");
//        t.domain("http://localhost:8080/leid_20151109");
    }
    private  void domain(String host) {
        // TODO Auto-generated method stub
        String HOST_ONLINE="http://search.lekan.letv.com";
        Field[] fields= this.getClass().getDeclaredFields();
        for(Field f:fields){
            Object o=null;
            try {
                o=f.get(this);
            }catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(f.getName()+":"+String.valueOf(o));
            boolean result=checkJson(doGet(HOST_ONLINE+String.valueOf(o)),doGet(host+String.valueOf(o)));
            System.out.println(result);
        }
        
        
    }
    private static JSONObject doGet(String url) {
        String result=getUrl(url,2000);
        if(StringUtils.isNotEmpty(result)){
            return JSONObject.parseObject(result);
        }else{
            System.out.println("程序异常");
            System.exit(1);
            return null;
        }
    }
    
    
    private static boolean checkJson(JSONObject first,JSONObject second) {
        boolean result=false;
        Set<String> firstKeys=first.keySet();
        if(first.size()==0&&0==second.size())
            result=true;
        for(String key:firstKeys){
            Object obj=first.get(key);
            if(!second.containsKey(key)){
            }else{
                if(obj instanceof JSONObject){
                    result=checkJson((JSONObject)obj,second.getJSONObject(key));
                }else if(obj instanceof JSONArray){
                    //比较数量
                    result = checkArray(second, key, obj);
                }else{
                    result=second.containsKey(key);
                }
            }
            if(!result){
                System.out.println("the "+String.valueOf(key)+" errors");
                return false;
            }
        }
        return result;
    }
    private static boolean checkArray(JSONObject second, String key, Object obj) {
        boolean result;
        JSONArray oo=(JSONArray)obj;
        result=oo.size()==second.getJSONArray(key).size();
        //对每个元素递归
        for(int i=0;i<oo.size();i++){
            if(oo.get(i) instanceof JSONObject){
                result=checkJson((JSONObject)oo.get(i), (JSONObject)(second.getJSONArray(key).get(i)));
            }else{
                result=String.valueOf(oo.get(i)).equals(String.valueOf(second.getJSONArray(key).get(i)));
            }
        }
        return result;
    }
    
    public static String getUrl(String urlStr,int time_out) {
        HttpURLConnection url_con = null;
        try {
          URL url = new URL(urlStr);
          url_con = (HttpURLConnection)url.openConnection();
          url_con.setDoOutput(true);
          url_con.setConnectTimeout(time_out);//毫秒
          url_con.setReadTimeout(time_out);
          return makeContent(url_con);
        }catch (Exception e) {
        }
        finally {
          if (url_con != null) {
            url_con.disconnect();
          }
        }
        return "";
      }
    private static String makeContent(final HttpURLConnection urlConnection) throws IOException{
        StringBuffer result = new StringBuffer();
        BufferedReader in=null;
        if(urlConnection.getResponseCode()==200){
            try{
                 in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String line = in.readLine();
                while (line != null) {
                    result.append(line).append("\n");
                    line = in.readLine();
                }
                in.close();
            } catch (IOException e) {
                throw e;
            } finally {
                if(null!=in){
                    in.close();
                }
            }
        }
        return result.toString();
    }
}
